/* 
 * File:   PackageControl.cpp
 * Author: Neo Siong Wei
 * 
 * Created on February 11, 2014, 9:04 PM
 */
#include "PackageControl.h"
#include "MailControl.h"

PackageControl::PackageControl ()
{
    ifstream infile;
    infile.open ("Package.txt", ios::in);
    
    char hType;
    int userID, pType, slot, cost;
    string loc;
    
    char tempstr[100];
	
    while (!infile.eof())
    {
	infile.getline(tempstr, 100);
	char *ptr = strtok (tempstr, "\n,");
		
	while (ptr != NULL)
	{
		hType = *ptr;
		ptr = strtok(NULL, "\n,");
			
		userID = atoi (ptr);
		ptr = strtok(NULL, "\n,");
			
		loc = ptr;
		ptr = strtok(NULL, "\n,");
			
		pType = atoi(ptr);
		ptr = strtok(NULL, "\n,");
			
		slot = atoi(ptr);
		ptr = strtok(NULL, "\n,");
			
		cost = atoi(ptr);
		ptr = strtok(NULL, "\n,");
			
		PackageData temp(hType, userID, loc, pType, slot, cost);
                packageData.push_back(temp);	   
	}	
    }
    infile.close();
}
    
PackageControl::~PackageControl ()
{
    ofstream outfile;
    outfile.open ("Package.txt", ios::out);
    
    for (unsigned int i = 0; i < packageData.size(); i++) 
    {
        outfile << packageData[i].getPType()   << "," 
		<< packageData[i].getPackID()  << ","
                << packageData[i].getDestName()<< ","
                << packageData[i].getHType()   << ","
                << packageData[i].getCapacity()<< ","
		<< packageData[i].getCost()    << "\r\n";

    } 
    outfile.close();
}

string PackageControl::ReturnPType (char pType)
{
     switch (pType)
     {
         case 'S': return "Std";
             break;
         default : return "Spec";
     }
}

string PackageControl::ReturnPName (int hType)
{
    switch (hType)
    {
        case 0: return "Small Adv";
            break;
        case 1: return "Tier Adv";
            break;
        case 2: return "Great Adv";
            break;
        default: return "Really Big ADV!";     
    }
}

string PackageControl::ReturnDays (int hType)
{
    switch (hType)
    {
        case 0: return "3D 2N";
            break;
        case 1: return "7D 6N";
            break;
        case 2: return "14D 13N";
            break;
        default: return "28D 27N";     
    }
}

void PackageControl::PrintBanner ()
{
    int temp;
    string banner;
    
    ifstream infile;
    infile.open ("Banner.txt", ios::in);
    
    while (infile >> temp)
    {
        getline (infile, banner);
        cout << banner <<endl;
    }
    infile.close();
}

PackageData PackageControl::returnPackage (int choice)
{
    int userID, hType, slot, cost;
    char pType;
    string location; 
    
    userID = packageData[choice - 1].getPackID();
    pType = packageData[choice - 1].getPType();
    location = packageData[choice - 1].getDestName();
    hType = packageData[choice - 1].getHType();
    slot = packageData[choice - 1].getCapacity();
    cost = packageData[choice - 1].getCost();
    
    PackageData singlePack(pType, userID, location, hType, slot, cost);
    
    return singlePack;
}

void PackageControl::ViewPackage (char role)
{
    char choice = 1;
    
    if (role == 'G')
    {      
        cout <<endl;
        cout << left << setw(5) << "NO."  
                << left << setw(7) << "TYPE"
                << left << setw(16) << "PACKAGES"
                << left << setw(8) << "SLOTS"
                << left << setw(6) << "COST" 
                << left << setw(20) << "LOCATION" <<endl <<endl;
        
        for (unsigned int i = 0; i < packageData.size() ; i++)
        {
            cout << left << setw (5) << i + 1 
                << left << setw(7) << ReturnPType (packageData[i].getPType())
                << left << setw(16) << ReturnPName(packageData[i].getHType())
                << left << setw(8) << packageData[i].getCapacity()
                << left << setw(6) << packageData[i].getCost()    
                << left << setw(1) << packageData[i].getDestName() <<endl;
        }
    }
    else 
    {
        PrintBanner ();
        cout << left << setw(5) << "NO."  
                << left << setw(10) << "DAY/NIGHT"
                << left << setw(16) << "PACKAGES"
                << left << setw(8) << "COST" 
                << left << setw(1) << "LOCATION" <<endl <<endl;
        
        for (unsigned int i = 0; i < packageData.size() ; i++)
        {
            cout << left << setw (5) << i + 1 
                << left << setw(10) << ReturnDays (packageData[i].getHType())
                << left << setw(16) << ReturnPName(packageData[i].getHType())
                << left << setw(8) << packageData[i].getCost()    
                << left << setw(1) << packageData[i].getDestName() <<endl;
        } 
    }
    cout <<endl;
}

bool PackageControl::CheckDupPack (string loc, int hType)
{ 
    for (int i = 0; i < packageData.size(); i++)
    {
        if (loc == packageData[i].getDestName() && hType == packageData[i].getHType())
            return true;
    }
    return false;
}

void PackageControl::CreatePackage ()
{
    int choice, userID, pType, slot, cost;
    char hType;
    string location; 
    
    cout << "ENTER Holiday Destination (35 characters) "
            <<endl << "Text: " <<endl;
    
    cin.clear();
    cin.ignore(80, '\n');
     
    while(!getline(cin, location) || !(location.size() <= 35 && location.size() > 0))    
    { 
        cout <<endl << "Invalid!!!" <<endl
                    << "Please renter: " <<endl;
    }
    
    cout <<endl;
    cout << "Which type of Package to create?" <<endl
            <<" 1) The Small Adventure" <<endl
            <<" 2) The Tiers Adventure" <<endl 
            <<" 3) The Great Adventure" <<endl
            <<" 4) The Really Big Adventure!" <<endl <<endl
            << "Your choice: ";
    
    while (!(cin >> choice) || !(choice >= 1 && choice <= 4))
    {
        cout << "Invalid Choice!!! Renter: ";
        cin.clear();
        cin.ignore(100,'\n');
    }
    
    if (choice == 4)
        hType = 'R';
    else
        hType = 'S';
 
    userID = packageData[packageData.size () - 1].getPackID () + 1;
    
    switch (choice)
    {
        case 1: pType = 0;
                break;
        case 2: pType = 1;
                break;
        case 3: pType = 2;
                break;
        default: pType = 3;
    }
    
    if (CheckDupPack(location, pType))
    {
        cout <<endl << "Entry Exist!!!";
                     return;
    }
  
   cout <<endl;
   cout << "ENTER total slots available (1-100): ";
    
   while (!(cin >> slot) || !(slot >= 1 && slot <=100))
   {
       cout << "Invalid Entry!!! Renter: ";
       cin.clear();
       cin.ignore(100,'\n');
   }

   cout <<endl;
   cout << "ENTER total cost for package (1-9999): ";
    
   while (!(cin >> cost) || !(cost >= 1 && cost <=9999))
   {
       cout << "Invalid Entry!!! Renter: ";
       cin.clear();
       cin.ignore(100,'\n');
   }
   
   PackageData temp(hType, userID, location, pType, slot, cost);
   packageData.push_back(temp);
   cout <<endl << "PACKAGE CREATED!!!" <<endl;
   
   //sends out brochure to all customers
   mailController m;
   m.sendBrochure("A new Package has been launched! Please select Make Bookings for more details!");
   
   ViewPackage ('G');
}

void PackageControl::EditPackage ()
{
    int choice, choice1, choice2, temp1;
    string temp2;
    
    ViewPackage ('G'); 
    cout <<endl << "Enter which row number to edit: ";
    
    while (!(cin >> choice) || !(choice >= 1 && choice <= packageData.size()))
    {
        cout << "Invalid Choice!!! Renter: ";
        cin.clear();
        cin.ignore(100,'\n');
    } 
    
    cout <<endl << "EDIT which portion?" <<endl <<endl
            << "  1) Package type" <<endl
            << "  2) Slot available" <<endl
            << "  3) Cost of package" <<endl
            << "  4) Location of holiday" <<endl <<endl
            << "Your choice: ";
    
    while (!(cin >> choice1) || !(choice1 >= 1 && choice1 <= 4))
    {
        cout << "Invalid Choice!!! Renter: ";
        cin.clear();
        cin.ignore(100,'\n');
    } 
    
    cout <<endl; 
    
    switch (choice1)
    {
        case 1: cout << "Change to which package?" <<endl
                        <<" 1) The Small Adventure" <<endl
                        <<" 2) The Tiers Adventure" <<endl 
                        <<" 3) The Great Adventure" <<endl
                        <<" 4) The Really Big Adventure!" <<endl <<endl
                        << "Your choice: ";
    
                while (!(cin >> choice2) || !(choice2 >= 1 && choice2 <= 4))
                {
                        cout << "Invalid Choice!!! Renter: ";
                        cin.clear();
                        cin.ignore(100,'\n');
                }
        
                if (CheckDupPack(packageData[choice - 1].getDestName(), choice2 - 1))
                {
                        cout <<endl << "Entry Exist!!!";
                        return;
                }
   
                switch (choice2)
                {
                        case 1: packageData[choice - 1].setHType(0);
                                break;
                        case 2: packageData[choice - 1].setHType(1);
                                break;
                        case 3: packageData[choice - 1].setHType(2);
                                break;
                        default: packageData[choice - 1].setHType(3);
                }
                
                if (choice2 == 4)
                        packageData[choice - 1].setPType('R');
                else
                        packageData[choice - 1].setPType('S');
                break;
                
        case 2: cout << "ENTER new slot available (1-100): ";
    
                while (!(cin >> temp1) || !(temp1 >= 1 && temp1 <=100))
                {
                         cout << "Invalid Entry!!! Renter: ";
                         cin.clear();
                         cin.ignore(100,'\n');
                }
                packageData[choice - 1].setCapacity(temp1);
                break;
                
        case 3: cout << "ENTER new cost for package (1-9999): ";
    
                while (!(cin >> temp1) || !(temp1 >= 1 && temp1 <=9999))
                {
                        cout << "Invalid Entry!!! Renter: ";
                        cin.clear();
                        cin.ignore(100,'\n');
                }
                packageData[choice - 1].setCost(temp1);
                break;
        default: cout <<endl << "ENTER new Holiday Destination (35 characters)"
                        <<endl << "Text: " <<endl;
    
                cin.clear();
                cin.ignore(1000,'\n');
    
                while(!getline(cin, temp2) || !(temp2.size() <= 35 && temp2.size() > 0))    
                {
                        cout <<endl << "Invalid!!!" <<endl
                                << "Please renter: " <<endl;
                }
                temp2 = " " + temp2;
                
                if (CheckDupPack(temp2, packageData[choice - 1].getHType()))
                {
                        cout <<endl << "Entry Exist!!!";
                        return;
                }
                packageData[choice - 1].setDestName(temp2);
    } 
    
    cout <<endl
            << "Package updated!!!" <<endl;
     ViewPackage ('G');
}

void PackageControl::DelPackage ()
{
    int choice;
    ViewPackage ('G'); 
    cout <<endl << "Enter which row number to delete: ";
    
    while (!(cin >> choice) || !(choice >= 1 && choice <= packageData.size()))
    {
        cout << "Invalid Choice!!! Renter: ";
        cin.clear();
        cin.ignore(100,'\n');
    } 
    
    packageData.erase (packageData.begin () + (choice - 1));
    
    cout <<endl << "Package deleted!!!" <<endl;
    ViewPackage ('G'); 
}


